//
//  DataSaveViewController.m
//  myselfTest
//
//  Created by Lucifer on 15-5-20.
//  Copyright (c) 2015年 xuxzhou. All rights reserved.
//

#import "DataSaveViewController.h"
#import "userDB.h"
#import "User.h"
#import "AddUserViewController.h"
@interface DataSaveViewController ()<UITableViewDataSource,UITableViewDelegate,addUserDataDelegate>
@property(nonatomic,strong)UITableView * tabView;
@property(nonatomic,strong)userDB * userDB;
@property(nonatomic,strong)NSMutableArray * userData;
@end

@implementation DataSaveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    userDB * db = [[userDB alloc] init];
    [db createDataBase];
     _userDB = [[userDB alloc] init];
    _userData = [NSMutableArray arrayWithArray:[_userDB findWithUid:nil limit:10000]];
    [self buildTabView];
    UIBarButtonItem * rightBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addUser:)];
    self.navigationItem.rightBarButtonItem = rightBtn;
}
-(void)buildTabView{
    _tabView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)-64)];
    _tabView.delegate=self;
    _tabView.dataSource=self;
    [self.view addSubview:_tabView];
    _tabView.tableFooterView=[[UIView alloc]init];
}
- (void) addUser:(id) sender {
   
    AddUserViewController * controller =  [[AddUserViewController alloc] init];
    controller.delegate=self;
    controller.indexPath=nil;
    [self.navigationController pushViewController:controller animated:YES];
}
#pragma mark - tab
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _userData.count;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * cellName = @"CELL";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellName];
    }
    User * user = [_userData objectAtIndex:indexPath.row];
    cell.textLabel.text = user.name;
    cell.detailTextLabel.text = user.headerImageUrlStr;
    return cell;
}


- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    User  * user = [_userData objectAtIndex:indexPath.row];
    AddUserViewController * controller = [[AddUserViewController alloc] initWithUser:user];
    controller.delegate=self;
    controller.indexPath=indexPath;
    [self.navigationController pushViewController:controller animated:YES];
}
- (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
/// 左右滑动所显示的文字
- (NSString *)tableView:(UITableView *)tableVie titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"干掉";
}
/// 当点击删除时，删除该条记录
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    // 如果是删除事件，则删除该条记录
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        User  * user = [_userData objectAtIndex:indexPath.row];
        [_userDB deleteUserWithId:user.uid];//删除
        [_userData removeObjectAtIndex:indexPath.row];
        [_tabView reloadData];
    }
}
-(void)addOrChangeUser:(User *)user AtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath==nil) {
        [_userData insertObject:user atIndex:0];
    }else{
        [_userData replaceObjectAtIndex:indexPath.row withObject:user];
    }
    [_tabView reloadData];
   



}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
